﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameJoltServices.Requests;
using GameJolt.Async;
#endregion

namespace GameJoltServices
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class ServicesManager
	{
		#region Variables
		private bool isUsingDevData = false;
		#endregion

		#region Properties
		internal bool IsUsingDevData
		{
			get
			{
				return isUsingDevData;
			}
		}

		public string GameID
		{
			get;
			private set;
		}

		public string PrivateKey
		{
			get;
			private set;
		}
		#endregion

		#region Constructors
		public ServicesManager(string gameID, string privateKey)
		{
			if(string.IsNullOrWhiteSpace(gameID))
				throw new ArgumentException("You must supply a valid game ID string.", "gameID");
			if(string.IsNullOrWhiteSpace(privateKey))
				throw new ArgumentException("You must supply a valid private key string.", "privateKey");

			if(gameID.ToLower() == "devgameid" && privateKey.ToLower() == "devprivatekey")
				isUsingDevData = true;

			this.GameID = gameID;
			this.PrivateKey = privateKey;
		}
		#endregion

		#region Methods
		public IAsyncResult BeginLogInUser(string username, string userToken, UserIndex userIndex = UserIndex.Any, AsyncCallback callback = null, object asyncState = null)
		{
			var request = new LogInRequest(this, username, userToken, userIndex);
			return request.Begin(callback, asyncState);
		}

		public LoggedInUser EndLogInUser(IAsyncResult result)
		{
			var joltResult = (AsyncResult<LoggedInUser>)result;
			var request = (LogInRequest)joltResult.CoreData;
			return request.End(result);
		}

		public LoggedInUser LogInUser(string username, string userToken, UserIndex userIndex = UserIndex.Any)
		{
			var request = new LogInRequest(this, username, userToken, userIndex);
			return request.Process(null);
		}

		public IAsyncResult LogInUser(string username, string userToken, UserIndex userIndex, Action<LoggedInUser> callback)
		{
			return BeginLogInUser(username, userToken, userIndex, (result) => callback(EndLogInUser(result)));
		}

		public static string GetDefaultCacheFolder(string gameFolder)
		{
#if PLATFORM_PC
			return System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games", gameFolder);
#elif PLATFORM_ANDROID
			return System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath, gameFolder);
#endif
		}
		#endregion
	}
}
